package com.jiukuaidao.zhulin.module.order;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.Toast;

import com.jiukuaidao.library.base.BaseFragment;
import com.jiukuaidao.library.base.BaseXrecyclerViewAdapter;
import com.jiukuaidao.library.util.ToastUtil;
import com.jiukuaidao.library.widget.recyclerview.XRecyclerView;
import com.jiukuaidao.zhulin.R;
import com.jiukuaidao.zhulin.adapter.OrderListAdapter;
import com.jiukuaidao.zhulin.data.TaskProvider;
import com.jiukuaidao.zhulin.entity.ApiOrderList;
import com.jiukuaidao.zhulin.module.order.contract.OrderListContract;
import com.jiukuaidao.zhulin.module.order.presenter.OrderListPresenter;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.List;

import butterknife.BindView;

/**
 * Author : PaiTaSuo
 * Date : 2016/6/2
 * Description :
 */
public class OrderListFragment extends BaseFragment<OrderListContract.Presenter> implements OrderListContract.View{
    @BindView(R.id.order_recycle)
    XRecyclerView mOrderRecycle;
    private String mLabelName;
    private String mStatus;
    private int mIndex = 1;
    private BaseXrecyclerViewAdapter mBaseAdapter;
    private int mRefreshOrLoad = OrderListContract.REFRESH;


//    @Override
//    public void onStart() {
//        super.onStart();
//        EventBus.getDefault().register(this);
//
//    }
//
//    @Override
//    public void onStop() {
//        EventBus.getDefault().unregister(this);
//        super.onStop();
//    }

    @Override
    protected void init() {
        new OrderListPresenter(TaskProvider.provideOrderListRepository(getActivity()), this);
        mPresenter.start();
    }

    @Override
    protected void handleBundle(Bundle bundle) {
        super.handleBundle(bundle);
        mLabelName = bundle.getString("lableName");
        mStatus = bundle.getString("status");
    }

    @Override
    protected View getTargetView() {
        return null;
    }

    @Override
    protected void onUserVisible() {
    }

    @Override
    protected void onUserInvisible() {

    }

    @Override
    protected int getContentLayoutId() {
        return R.layout.fragment_order;
    }


    @Override
    public int getIndex() {
        return mIndex;
    }

    @Override
    public String getStatus() {
        return mStatus;
    }

    @Override
    public void orderList(List<ApiOrderList.OrderList> orderList) {
        if (null != mBaseAdapter) {
            mBaseAdapter.setData(orderList);
            mBaseAdapter.notifyDataSetChanged();
        } else {
            initOrderList(orderList);
        }
    }

    public void initOrderList(List<ApiOrderList.OrderList> list) {
        mBaseAdapter = new OrderListAdapter(getActivity(), R.layout.item_order_list, list, mOrderRecycle);
        mOrderRecycle.setLayoutManager(new LinearLayoutManager(getActivity()));
        mOrderRecycle.setAdapter(mBaseAdapter);
        mOrderRecycle.setOnRefreshListener(new XRecyclerView.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mIndex = 1;
                mRefreshOrLoad = OrderListContract.REFRESH;
                mOrderRecycle.refreshComplete();
                mPresenter.start();
            }

            @Override
            public void onLoadMore() {
                mRefreshOrLoad = OrderListContract.LOADMORE;
                mIndex++;
                mOrderRecycle.loadMoreComplete();
                mPresenter.start();
            }
        });
    }


//    @Subscribe
//    public void onEventRefresh(){
//
//        mIndex = 1;
//        mRefreshOrLoad = OrderListContract.REFRESH;
//        mOrderRecycle.refreshComplete();
//        mPresenter.start();
//
//    }

    @Override
    public void toast(String msg) {
        ToastUtil.show(msg, Toast.LENGTH_SHORT);
    }

    @Override
    public int refreshOrLoad() {
        return mRefreshOrLoad;
    }


}
